<template>
  <div class="dorm-detail-main">
    <el-dialog
      title="宿舍信息"
      :visible.sync="dialogVisible"
      width="36%"
      :before-close="handleClose"
    >
      <span></span>
      <el-form ref="dormForm" :model="form" label-width="80px" :rules="rules">
        <el-card shadow="never" class="card">
          <div slot="header">
            <span>基本信息</span>
          </div>
          <el-form-item label="宿舍名称" prop="name">
            <el-input v-model="form.name" size="small" style="width: 200px" />
          </el-form-item>
          <el-form-item label="宿舍类型" prop="type">
            <el-radio v-model="form.type" :label="0">男生宿舍</el-radio>
            <el-radio v-model="form.type" :label="1">女生宿舍</el-radio>
            <el-radio v-model="form.type" :label="2">未定</el-radio>
          </el-form-item>
          <el-form-item label="宿舍位置" prop="position">
            <el-input
              v-model="form.position"
              size="small"
              style="width: 200px"
            />
          </el-form-item>
          <el-form-item label="入住日期" prop="startTime">
            <el-date-picker
              v-model="form.startTime"
              value-format="yyyy-MM-dd"
              type="date"
              size="small"
              placeholder="选择日期"
            />
          </el-form-item>
          <el-form-item label="离开日期">
            <el-date-picker
              v-model="form.endTime"
              value-format="yyyy-MM-dd"
              type="date"
              size="small"
              placeholder="选择日期"
            />
          </el-form-item>
          <el-form-item label="宿舍描述">
            <el-input
              v-model="form.bak"
              size="normal"
              clearable
              type="textarea"
            ></el-input>
          </el-form-item>
        </el-card>
      </el-form>
      <span slot="footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="doSave">确认</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  dormSave as dormSaveApi,
  dormDetail as dormDetailApi,
  dormUpdate as dormUpdateApi,
} from "@/api/xy/dorm";
export default {
  name: "xydormDetail",
  data() {
    return {
      dialogVisible: false, // 弹框是否显示
      form: {},
      rules: {
        name: [
          { required: true, message: "请输入名称", trigger: "blur" },
          { min: 3, max: 8, message: "长度在 3 到 8 个字符", trigger: "blur" },
        ],
        type: [{ required: true, message: "请选择类型", trigger: "blur" }],
        position: [{ required: true, message: "请填写地址", trigger: "blur" }],
        startTime: [{ required: true, message: "请选择时间", trigger: "blur" }],
      },
    };
  },
  methods: {
    openDialog(id) {
      this.dialogVisible = true;
      console.log(this.form);
      //   如果传递了id  说明是更新
      if (id) {
        dormDetailApi({ id }).then((res) => {
          console.log(res);
          this.form = res.data;
        });
      }
    },
    doSave() {
      var api = null;
      var xydormInfo = this.form;
      var data = {
        xydormInfo,
      };
      if (this.form.id) {
        api = dormUpdateApi;
      } else {
        api = dormSaveApi;
      }
      this.$refs.dormForm.validate((valid) => {
        if (valid) {
          api(data).then((res) => {
            console.log(res);
            // 友好提示
            this.$message.success("添加成功!");
            // 1. 清空表单
            this.$refs.dormForm.resetFields();
            // 2. 关闭弹窗
            this.dialogVisible = false;
            // 3. 刷新父类列表  $emit
            this.$emit("refrush");
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    handleClose(done) {
      this.$confirm("确认关闭？")
        .then((_) => {
          this.form = {};
          done();
        })
        .catch((_) => {});
    },
  },
};
</script>

<style lang="scss" scoped>

.dorm-detail-main {
  .card {
    border: none;
  }
}
</style>